<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>信息展示页</title>

	<script src="js/jquery-3.6.0.min.js"></script>
	<script src="js/axios-0.18.0.js"></script>
</head>
<body>
	<input type="text"  id="sou">
	<input type="button" value="搜索" onclick="sousuo()">


	<table>
		<thead>
			<tr>
				<th>id</th>
				<th>username</th>
				<th>password</th>
			</tr>
		</thead>
		<tbody id="tbody1">

		</tbody>
	</table>
</body>

<script>
	$(function(){
		axios.get("GetMessageServlet").then(function (resp){
			let arr = resp.data;
			for(let i = 0;i < arr.length;i++){
				createTr(arr[i]);
			}
		});
	});

	function createTr(e){
		let tr1 = $("<tr></tr>");

		let td1 = $("<td></td>").text(e.id);
		let td2 = $("<td></td>").text(e.username);
		let td3 = $("<td></td>").text(e.password);

		tr1.append(td1).append(td2).append(td3);

		$("#tbody1").append(tr1);
	}

	function sousuo(){
		let username = $("#sou").val();
		axios.get("GetUserServlet?username=" + username).then(function (resp){
			let arr = resp.data;

			if(arr.length != 0){
				alert("该用户存在");
			}else{
				alert("该用户未注册");
				return ;
			}

			// 把原本tbody里的内容清空掉
			$("#tbody1").empty();

			for(let i = 0;i < arr.length;i++){
				createTr(arr[i]);
			}
		});
	}

</script>



</html>